#!/bin/bash

source "$(
  cd "$(dirname "$0")" || exit 1
  pwd
)/common.sh"

function download_test_case_data() {
  TEST_CASE_DIR="${SCRIPT_DIR}/TestCase"
  safely_create_a_directory "${TEST_CASE_DIR}" || return 1
  wget -c -O "${TEST_CASE_DIR}/kallisto-0.48.0.tar.gz" https://github.com/pachterlab/kallisto/archive/v0.48.0.tar.gz
  if [ $? -eq 1 ]; then
    echo -e "\033[1;31;1mTest case download failed,Please rerun the current script.\033[0m"
    return 1
  fi
  tar -C "${TEST_CASE_DIR}" \
    -xzf "${TEST_CASE_DIR}/kallisto-0.48.0.tar.gz" \
    kallisto-0.48.0/test/reads_1.fastq.gz \
    kallisto-0.48.0/test/reads_2.fastq.gz \
    kallisto-0.48.0/test/transcripts.fasta.gz \
    kallisto-0.48.0/test/transcripts.gtf.gz \
    kallisto-0.48.0/test/chrom.txt \
    --strip-components 2 --no-same-owner
  if [ $? -eq 1 ]; then
    echo -e "\033[1;31;1mExtraction failed.\033[0m"
    return 1
  fi
  rm -rf "${TEST_CASE_DIR}/kallisto-0.48.0.tar.gz" || return 1
}

function main() {
  is_expected_architecture x86_64 || return 1
  set_script_dir || return 1
  download_test_case_data || return 1
  conda_initialize "$1"
  create_and_activate_conda_env bioconda || return 1
  local EXPECTED_DIR
  EXPECTED_DIR="${SCRIPT_DIR}/Expected"
  generate_execution_result_data "${EXPECTED_DIR}" "${TEST_CASE_DIR}"
  if [[ $? -eq 0 ]]; then
    echo -e "\033[1;32;1mSuccessfully generated expected data.\033[0m"
  else
    echo -e "\033[1;31;1mFailed to generate expected data.\033[0m"
    delete_a_directory_or_file "${TEST_CASE_DIR}"
    delete_a_directory_or_file "${EXPECTED_DIR}"
  fi
}

# /bin/bash ~/hpc/Anaconda/bioconda/kallisto/0.48.0/h0d531b0_1/test/generate_expected_data.sh /root/anaconda3
main "$@"
